﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using EntitySpaces.Interfaces;
using yncTech.Common;
using yncTech.BusinessObjects;

public partial class Admin_Fin_Fin004a : AdminBasePage
{
    decimal shipID = -1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString[FinShippingMetadata.ColumnNames.ShipID] != null)
        {
            shipID = decimal.Parse(Request.QueryString[FinShippingMetadata.ColumnNames.ShipID]);
        }

        try
        {
            using (esTransactionScope tr = new esTransactionScope())
            {
                FinShipping fsp = new FinShipping();
                fsp.LoadByPrimaryKey(shipID);
                switch (ActionType)
                {
                    case OperationType.Add:
                        fsp.ShippingStatus = ConstantValue.ShippingStatusCode.Confirmed;
                        fsp.ReceivedBy = LoginUserInfo.CurrentUser.UserName;
                        fsp.ReceivedDttm = DateTime.Now;
                        fsp.UpdatedBy = LoginUserInfo.CurrentUser.LoginName;
                        fsp.UpdatedDttm = DateTime.Now;
                        fsp.Save();

                        //确认后将box的location改成 ship to 

                        FinShippingBoxesCollection fsb = new FinShippingBoxesCollection();
                        fsb.Query.Where(fsb.Query.ShipID == shipID);
                        fsb.Query.Load();

                        var boxIDs = fsb.Select(r => r.BoxID.Value).ToArray();

                        FinBoxCollection fbs = new FinBoxCollection();
                        fbs.Query.Where(fbs.Query.BoxID.In(boxIDs));
                        fbs.Query.Load();

                        foreach (var item in fbs)
                        {
                            item.StorageStatus = ConstantValue.StorageStatusCode.InStock;
                            item.BoxLocation = fsp.ToDestination;
                            item.UpdatedBy = LoginUserInfo.CurrentUser.LoginName;
                            item.UpdatedDttm = DateTime.Now;
                        }
                        fbs.Save();

                        //将盒中的凭证也设置为:待转移

                        FinBoxReceiptsCollection boxRecs = new yncTech.BusinessObjects.FinBoxReceiptsCollection();
                        boxRecs.Query.Where(boxRecs.Query.BoxID.In(boxIDs));
                        boxRecs.Query.Load();

                        FinReceiptCollection receipts = new yncTech.BusinessObjects.FinReceiptCollection();
                        receipts.Query.Where(receipts.Query.ReceiptID.In(boxRecs.Select(r => r.ReceiptID.Value).ToArray()));
                        receipts.Query.Load();
                        foreach (var rec in receipts)
                        {
                            rec.StorageStatus = ConstantValue.StorageStatusCode.InStock;
                            rec.StorageLocation = fsp.ToDestination;
                            rec.TransferRecord = (rec.TransferRecord ?? 0) + 1;
                            rec.UpdatedBy = LoginUserInfo.CurrentUser.LoginName;
                            rec.UpdatedDttm = DateTime.Now;
                        }
                        receipts.Save();



                        break;
                    case OperationType.Delete:
                        fsp.ShippingStatus = ConstantValue.ShippingStatusCode.Cancelled;
                        fsp.UpdatedBy = LoginUserInfo.CurrentUser.LoginName;
                        fsp.UpdatedDttm = DateTime.Now;
                        fsp.Save();

                        //取消后将box的状态改为InStock

                        FinShippingBoxesCollection fsb1 = new FinShippingBoxesCollection();
                        fsb1.Query.Where(fsb1.Query.ShipID == shipID);
                        fsb1.Query.Load();

                        var boxIDs1 = fsb1.Select(r => r.BoxID.Value).ToArray();

                        FinBoxCollection fbs1 = new FinBoxCollection();
                        fbs1.Query.Where(fbs1.Query.BoxID.In(boxIDs1));
                        fbs1.Query.Load();

                        foreach (var item in fbs1)
                        {
                            item.StorageStatus = ConstantValue.StorageStatusCode.InStock;
                            item.UpdatedBy = LoginUserInfo.CurrentUser.LoginName;
                            item.UpdatedDttm = DateTime.Now;
                        }
                        fbs1.Save();

                        break;
                }
                tr.Complete();
            }
            WebHelper.GotoParent();
        }
        catch (Exception ex)
        {
            WebHelper.GotoParent(ex.Message);
        }

    }


}